#!/usr/bin/python3
import sys

def unfold_both(split):
    record, sequence = split
    record = '?'.join([record] * 5)
    sequence = [int(n) for n in ','.join([''.join([sequence])] * 5).split(',')]
    print(record, sequence)
    return record, sequence

def resolve(record, sequence, cache):
    if len(sequence) == 0:
        return 1 if '#' not in record else 0
    block = sequence[0]
    count = 0
    for i in range(len(record)):
        left, middle, right = record[:i], record[i:i + block], record[i + block:]
        if '#' in left:
            break
        if '.' in middle:
            continue
        if len(middle) != block:
            continue
        if (len(left) > 0 and left[-1] == '#') or (len(right) > 0 and right[0] == '#'):
            continue
        #print(f'{left} + {middle} + {right} {sequence} {count}')
        step_record, step_sequence = record[i + block + 1:], sequence[1:]
        combined = (step_record, str(step_sequence))
        if combined in cache:
            count += cache[combined]
        else:
            result = resolve(step_record, step_sequence, cache)
            cache[combined] = result
            count += result
    return count

def part1(unfold=False):
    ways = 0
    cache = {}
    for line in sys.stdin:
        split = line.strip().split(' ', 2)
        record = split[0]
        sequence = [int(n) for n in split[1].split(',')]
        if unfold:
            record, sequence = unfold_both(split)
        ways += resolve(record, sequence, cache)
    print(f'{ways} ways')

if sys.argv[1] in '1':
    part1()
else:
    part1(unfold=True)
